package com.yin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yin.common.util.PageResult;
import com.yin.entity.Personnel;
import com.yin.entity.Room;
import com.yin.mapper.PersonnelMapper;
import com.yin.service.IPersonnelService;
import com.yin.service.IRoomService;
import com.yin.vo.PersonnelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yp
 * @since 2025-01-14
 */
@Service
public class PersonnelServiceImpl extends ServiceImpl<PersonnelMapper, Personnel> implements IPersonnelService {
    @Autowired
    private IRoomService roomService;
    
    // 1. 分页查询
    @Override
    public PageResult<Personnel> findByPage(Integer page, Integer pageSize) {
        // 1. 开始分页
        Page<Personnel> page1 = this.page(new Page<>(page, pageSize));
        
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }
    
    // 3. 条件带分页查询
    @Override
    public PageResult<Personnel> search(Integer page, Integer pageSize, PersonnelVo vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Personnel> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getPname()), "pname", vo.getPname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getRid() != null && vo.getRid() != 0, "rid", vo.getRid());
        // 1.2 开始分页查询
        Page<Personnel> page1 = this.page(new Page<>(page, pageSize), wrapper);
        // 1.3 处理学生所在班级名称
        page1.getRecords().forEach(st -> {
            Room room = roomService.getById(st.getRid());
            st.setRaddr(room.getRaddr());
        });
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }
}
